package com.biboheart.adapter.express.support;

import com.biboheart.adapter.express.actuator.GenericActuator;
import com.biboheart.adapter.support.utils.ObjectTypeUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

final class DivisionActuator implements GenericActuator {
    @Override
    public Object execute(Object source, Object... args) {
        if (null == source || args.length == 0) {
            return null;
        }
        Object divisor = args[0];
        if (!(source instanceof Number) || !(divisor instanceof Number)) {
            return null;
        }
        int scale = args.length > 1 ? ObjectTypeUtils.convertType(args[1], Integer.class) : 2;
        return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Double.class)).divide(BigDecimal.valueOf(ObjectTypeUtils.convertType(divisor, Double.class)), scale, RoundingMode.HALF_UP).doubleValue();
    }
}
